<html>

<head>

</head>

<body>
    <h2>向选中的单元格区域写入内容/公式</h2>
    <div id="ctlBox">
        <div>
            <input value="" name="v" id="write">
            <select name="type" id="type">
                <option value="formula">公式</option>
                <option value="str" selected="selected">字符</option>
                <option value="numb">数值</option>
            </select>
            <button onclick="gogogo()">写入</button>
        </div>
        <div id="note">

        </div>
    </div>
    <hr />
    <div id="rangeInfo">

    </div>
    <script type="text/javascript">
        function gogogo() {
            try{
            let ll = document.getElementById('note')
            let selection = wps.Application.ActiveWindow.RangeSelection;
            let inpv = document.getElementById('write').value;
            let domSele = document.getElementById('type');
            let a = domSele.selectedIndex
            let c = domSele.options[a].value
            ll.innerText = `${inpv},${c}`
           
            if (inpv !== "") {
                switch (c) {
                    case "formula":
                        selection.Formula = inpv;
                        break;
                    case "numb":
                        selection.Value2 = Number(inpv);
                        break;
                    default:
                        selection.Value2 = inpv;
                        break;
                }
            } else {
                ll.innerText = "貌似啥也没填呢，我的宝……"
            }
        }catch (e){
            console.log(e)
        }
        }
    </script>
    <script type="text/javascript">
        wps.ApiEvent.AddApiEventListener("SheetSelectionChange", () => {
            let selec = wps.Application.ActiveWindow.RangeSelection;
            let info = [
                `address: ${selec.Address()}`,
                `工作表: ${selec.Parent.Name}`,
                `Value2 : ${selec.Value2}`,
                `公式 ： ${selec.Formula}`,
                `Count:  ${selec.Count}`,
                `Interior: Color-> ${selec.Interior.Color} ColorIndex-> ${selec.Interior.ColorIndex}`,
                `Font: Name -> ${selec.Font.Name}; Size -> ${selec.Font.Size}; Color: -> ${selec.Font.Color}; ColorIndex -> ${selec.Font.ColorIndex}`,
                `ValueAsJSON_stringify: ${JSON.stringify(selec.Value())}`
            ];
            document.getElementById('rangeInfo').innerHTML = info.map(o => `<p>${o}</p>`).join('\n')
        })
    </script>
    
</body>

</html>